﻿using PARK.Dependency;
using SqlSugar;
using System;
using PARK.DataEncryption;
using PARK.BussinCache.Interfaces;
using PARK.BussinCache.Entitys.Dto;
using PARK.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using uni.ubi.open.sdk.Model;
using uni.ubi.open.sdk.Uts;
using JNPF.Logging;
using PARK.Common.Helper;

namespace PARK.BussinCache
{
    /// <summary>
    /// 人员管理服务
    /// </summary>
    public class UniubiSdkRepository : IUniubiSdkRepository, ITransient
    {

        /// <summary>
        /// 接口域名地址
        /// </summary>
        private readonly string _ubi_apiurl = "http://wo-api.uni-ubi.com";

        private readonly SqlSugarScope _db;
        private readonly IParkCacheRepository _parkCacheRepository;

        /// <summary>
        /// 初始化一个<see cref="UniubiSdkRepository"/>类型的新实例
        /// </summary>
        public UniubiSdkRepository(IParkCacheRepository parkCacheRepository)
        {
            _parkCacheRepository = parkCacheRepository;
        }

        #region 鉴权接口
        /// <summary>
        /// 鉴权接口
        /// </summary>
        /// <returns></returns>
        public uniubiAppInfo auth(string CompanyId)
        {
            new LoggEx().uniEvtlog("鉴权接口->0->_appInfo->" + CompanyId);
            var _appInfo = _parkCacheRepository.GetCacheUniubiAppInfo(CompanyId);
            if (_appInfo != null)
            {
                new LoggEx().uniEvtlog("鉴权接口->1->_appInfo->" + _appInfo.ToJson());
                if (_appInfo.uni_tokenLastTime.AddHours(20) < DateTime.Now)
                {
                    //toke已过期，重新获取token
                    var _appKey = _appInfo.uni_appKey;
                    var _timestamp = ToUnixTime(DateTime.Now).ToString();
                    var _sign = MD5Encryption.Encrypt(_appKey + _timestamp + _appInfo.uni_appSecret);    //MD5-32(appKey+timestamp+appSecret)，MD5 加密，32 位小写 #返回数据:
                    var _outmsg = "";
                    var retObject = WebApiHelper.Get(_ubi_apiurl.Replace("https", "http") + "/v1/" + _appInfo.uni_projectGuid + "/auth", "", _appKey, _timestamp, _sign, out _outmsg);
                    if (retObject != null)
                    {
                        if (retObject["success"].ToString().ToLower() == "true")
                        {
                            //成功
                            _appInfo.uni_token = retObject["data"].ToString();
                            _appInfo.uni_tokenLastTime = DateTime.Now;

                            new LoggEx().uniEvtlog("鉴权接口->2->_appInfo->" + _appInfo.ToJson());
                            return _appInfo;
                        }
                        else
                        {
                            //失败
                            new LoggEx().uniEvtlog("宇泛平台,鉴权接口->获取token失败:" + _outmsg);
                        }
                    }
                }
                else
                {
                    new LoggEx().uniEvtlog("鉴权接口->3>_appInfo->" + _appInfo.ToJson());
                    return _appInfo;
                }
            }
            new LoggEx().uniEvtlog("鉴权接口->4>_appInfo->null");
            return null;
        }
        /// <summary>
        /// 获取从1970年1月1号到一个时间点的毫秒数【毫秒时间戳】
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static long ToUnixTime(DateTime date)
        {
            var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            return Convert.ToInt64((date.ToUniversalTime() - epoch).TotalMilliseconds);
        }
        #endregion

        #region 设备管理
        /// <summary>
        /// 添加设备
        /// </summary>
        /// <param name="deviceNo">设备序列号</param>
        /// <param name="deviceName">设备名称</param>
        /// <param name="CompanyId">所属公司Id</param>
        /// <returns></returns>
        public dfpOutPut deviceCreate(string deviceNo, string deviceName, string CompanyId)
        {
            var _output = new dfpOutPut();
            try
            {
                var _appInfo = auth(CompanyId);
                if (_appInfo == null)
                {
                    _output.result = 0;
                    _output.code = "token_err";
                    _output.msg = "获取宇泛token失败";
                    _output.data = "";
                    return _output;
                }
                var _ubi_input = new Device_ubi()
                {
                    deviceNo = deviceNo,
                    name = deviceName,
                    tag = "HST",
                    source = "000000",
                    addition = "{'recType':'1'}"
                };
                new LoggEx().uniEvtlog("添加设备,->deviceCreate()->请求参数->" + _ubi_input.ToJson());
                var _CourseJson = JsonConvert.SerializeObject(_ubi_input);
                JObject retObject = WebApiHelper.Post(_ubi_apiurl.Replace("https", "http") + "/v2/device/create", _CourseJson, _appInfo.uni_token, _appInfo.uni_projectGuid);
                if (retObject != null)
                {
                    new LoggEx().uniEvtlog("调用宇泛接口->编添加设备辑设备->deviceCreate(),接口返回:" + retObject.ToJson());
                    if (retObject["success"].ToString().ToLower() == "true")
                    {
                        //成功
                        _output.result = 1;
                        _output.code = retObject["code"].ToString();
                        _output.msg = "添加设备成功";
                        _output.data = "";
                    }
                    else
                    {
                        //失败
                        _output.result = 0;
                        _output.code = retObject["code"].ToString();
                        _output.msg = retObject["msg"].ToString();
                        _output.data = "";
                    }
                }
            }
            catch (Exception ex)
            {
                _output.result = -1;
                _output.code = "err";
                _output.msg = "发生异常";
                _output.data = "";
                new LoggEx().errlog("调用宇泛接口->添加设备,异常:" + ex);
            }
            new LoggEx().uniEvtlog(_output.ToJson());
            return _output;
        }
        /// <summary>
        /// 编辑设备
        /// </summary>
        /// <param name="deviceNo"></param>
        /// <param name="deviceName"></param>
        /// <returns></returns>
        public dfpOutPut deviceEdit(string deviceNo, string deviceName, string CompanyId)
        {
            var _output = new dfpOutPut();
            try
            {
                var _appInfo = auth(CompanyId);
                if (_appInfo == null)
                {
                    _output.result = 0;
                    _output.code = "token_err";
                    _output.msg = "获取宇泛token失败";
                    _output.data = "";
                    return _output;
                }
                var _ubi_input = new Device_ubi()
                {
                    deviceNo = deviceNo,
                    name = deviceName,
                    tag = "HST",
                    source = "000000",
                    addition = "{'recType':'1'}"
                };
                new LoggEx().uniEvtlog("编辑设备,->deviceEdit()->请求参数->" + _ubi_input.ToJson());
                var _CourseJson = JsonConvert.SerializeObject(_ubi_input);
                JObject retObject = WebApiHelper.Post(_ubi_apiurl.Replace("https", "http") + "/v2/device/update", _CourseJson, _appInfo.uni_token, _appInfo.uni_projectGuid);
                if (retObject != null)
                {
                    new LoggEx().uniEvtlog("调用宇泛接口->编辑设备->deviceEdit(),接口返回:" + retObject.ToJson());
                    if (retObject["success"].ToString().ToLower() == "true")
                    {
                        //成功
                        _output.result = 1;
                        _output.code = retObject["code"].ToString();
                        _output.msg = "修改设备成功";
                        _output.data = "";
                    }
                    else
                    {
                        //失败
                        _output.result = 0;
                        _output.code = retObject["code"].ToString();
                        _output.msg = retObject["msg"].ToString();
                        _output.data = "";
                    }
                }
            }
            catch (Exception ex)
            {
                _output.result = -1;
                _output.code = "err";
                _output.msg = "发生异常";
                _output.data = "";
                new LoggEx().errlog("调用宇泛接口->修改设备,异常:" + ex);
            }
            new LoggEx().uniEvtlog(_output.ToJson());
            return _output;
        }
        /// <summary>
        /// 删除设备
        /// </summary>
        /// <param name="deviceNo">设备序列号</param>
        /// <returns></returns>
        public dfpOutPut deviceDelete(string deviceNo, string CompanyId)
        {
            var _output = new dfpOutPut();
            try
            {
                var _appInfo = auth(CompanyId);
                if (_appInfo == null)
                {
                    _output.result = 0;
                    _output.code = "token_err";
                    _output.msg = "获取宇泛token失败";
                    _output.data = "";
                    return _output;
                }
                var _ubi_delete = new Device_ubi_delete()
                {
                    deviceNo = deviceNo,
                    source = "000000"
                };
                new LoggEx().uniEvtlog("删除设备,->deviceDelete()->请求参数->" + _ubi_delete.ToJson());
                var _CourseJson = JsonConvert.SerializeObject(_ubi_delete);
                JObject retObject = WebApiHelper.Post(_ubi_apiurl.Replace("https", "http") + "/v2/device/delete", _CourseJson, _appInfo.uni_token, _appInfo.uni_projectGuid);
                if (retObject != null)
                {
                    new LoggEx().uniEvtlog("调用宇泛接口->删除设备->deviceDelete(),接口返回:" + retObject.ToJson());
                    if (retObject["success"].ToString().ToLower() == "true")
                    {
                        //成功
                        _output.result = 1;
                        _output.code = retObject["code"].ToString();
                        _output.msg = "删除设备成功";
                        _output.data = "";
                    }
                    else
                    {
                        //失败
                        _output.result = 0;
                        _output.code = retObject["code"].ToString();
                        _output.msg = retObject["msg"].ToString();
                        _output.data = "";
                    }
                }
            }
            catch (Exception ex)
            {
                _output.result = -1;
                _output.code = "err";
                _output.msg = "发生异常";
                _output.data = "";
                new LoggEx().errlog("调用宇泛接口->删除设备,异常:" + ex);
            }
            new LoggEx().uniEvtlog(_output.ToJson());
            return _output;
        }
        /// <summary>
        /// 查询设备在线状态
        /// </summary>
        /// <param name="deviceNos">设备序列号s</param>
        /// <returns></returns>
        public dfpOutPut onlineState(string deviceNos, string CompanyId)
        {
            ///v2/device/onlineState
            ///
            return null;
        }
        /// <summary>
        /// 同步设备
        /// </summary>
        /// <param name="deviceNo">设备序列号</param>
        /// <returns></returns>
        public dfpOutPut deviceSync(string deviceNo, string CompanyId)
        {
            var _output = new dfpOutPut();
            try
            {
                var _appInfo = auth(CompanyId);
                if (_appInfo == null)
                {
                    _output.result = 0;
                    _output.code = "token_err";
                    _output.msg = "获取宇泛token失败";
                    _output.data = "";
                    return _output;
                }
                var _ubi_Sync = new Device_ubi_delete()
                {
                    deviceNo = deviceNo,
                    source = "000000"
                };
                new LoggEx().uniEvtlog("同步设备,->deviceSync()->请求参数->" + _ubi_Sync.ToJson());
                var _CourseJson = JsonConvert.SerializeObject(_ubi_Sync);
                JObject retObject = WebApiHelper.Post(_ubi_apiurl.Replace("https", "http") + "/v2/device/detail", _CourseJson, _appInfo.uni_token, _appInfo.uni_projectGuid);
                if (retObject != null)
                {
                    new LoggEx().uniEvtlog("调用宇泛接口->同步设备->deviceSync(),接口返回:" + retObject.ToJson());
                    if (retObject["success"].ToString().ToLower() == "true")
                    {
                        //成功
                        _output.result = 1;
                        _output.code = retObject["code"].ToString();
                        _output.msg = "同步设备成功";
                        _output.data = retObject.ToJson();
                    }
                    else
                    {
                        //失败
                        _output.result = 0;
                        _output.code = retObject["code"].ToString();
                        _output.msg = retObject["msg"].ToString();
                        _output.data = "";
                    }
                }
            }
            catch (Exception ex)
            {
                _output.result = -1;
                _output.code = "err";
                _output.msg = "发生异常";
                _output.data = "";
                new LoggEx().errlog("调用宇泛接口->同步设备,异常:" + ex);
            }
            new LoggEx().uniEvtlog(_output.ToJson());
            return _output;
        }
        #endregion

        #region 人员管理
        /// <summary>
        /// 添加人员
        /// </summary>
        /// <param name="JobNum">工号</param>
        /// <param name="RealName">姓名</param>
        /// <param name="cardNo">卡号</param>
        /// <returns></returns>
        public dfpOutPut eyeCreate(string JobNum, string RealName, string cardNo, string CompanyId)
        {
            var _output = new dfpOutPut();
            try
            {
                var _appInfo = auth(CompanyId);
                if (_appInfo == null)
                {
                    _output.result = 0;
                    _output.code = "token_err";
                    _output.msg = "获取宇泛token失败";
                    _output.data = "";
                    return _output;
                }
                var _ubi_employee = new employee_ubi()
                {
                    name = RealName,
                    tag = JobNum,
                    cardNo = cardNo,
                    addition = ""
                };
                new LoggEx().uniEvtlog("添加人员,->eyeCreate()->请求参数->" + _ubi_employee.ToJson());
                var _CourseJson = JsonConvert.SerializeObject(_ubi_employee);
                JObject retObject = WebApiHelper.Post(_ubi_apiurl.Replace("https", "http") + "/v2/admit/create", _CourseJson, _appInfo.uni_token, _appInfo.uni_projectGuid);
                if (retObject != null)
                {
                    new LoggEx().uniEvtlog("调用宇泛接口->添加人员->eyeCreate(),接口返回:" + retObject.ToJson());
                    if (retObject["success"].ToString().ToLower() == "true")
                    {
                        //成功
                        _output.result = 1;
                        _output.code = retObject["code"].ToString();
                        _output.msg = "添加人员成功";
                        _output.data = retObject["data"].ToString();
                    }
                    else
                    {
                        //失败
                        _output.result = 0;
                        _output.code = retObject["code"].ToString();
                        _output.msg = retObject["msg"].ToString();
                        _output.data = "";
                    }
                }
            }
            catch (Exception ex)
            {
                _output.result = -1;
                _output.code = "err";
                _output.msg = "发生异常";
                _output.data = "";
                new LoggEx().errlog("调用宇泛接口->添加人员,异常:" + ex);
            }
            new LoggEx().uniEvtlog(_output.ToJson());
            return _output;
        }
        /// <summary>
        /// 编辑人员
        /// </summary>
        /// <param name="admitGuid">宇泛平台用户Id</param>
        /// <param name="JobNum">工号</param>
        /// <param name="RealName">姓名</param>
        /// <param name="cardNo">卡号</param>
        /// <returns></returns>
        public dfpOutPut eyeEdit(string admitGuid, string JobNum, string RealName, string cardNo, string CompanyId)
        {
            var _output = new dfpOutPut();
            try
            {
                var _appInfo = auth(CompanyId);
                if (_appInfo == null)
                {
                    _output.result = 0;
                    _output.code = "token_err";
                    _output.msg = "获取宇泛token失败";
                    _output.data = "";
                    return _output;
                }
                var _ubi_employee = new employee_ubi_update()
                {
                    admitGuid = admitGuid,
                    name = RealName,
                    tag = JobNum,
                    cardNo = cardNo,
                    addition = ""
                };
                new LoggEx().uniEvtlog("编辑人员,->eyeEdit()->请求参数->" + _ubi_employee.ToJson());
                var _CourseJson = JsonConvert.SerializeObject(_ubi_employee);
                JObject retObject = WebApiHelper.Post(_ubi_apiurl.Replace("https", "http") + "/v2/admit/update", _CourseJson, _appInfo.uni_token, _appInfo.uni_projectGuid);
                if (retObject != null)
                {
                    new LoggEx().uniEvtlog("调用宇泛接口->编辑人员->eyeEdit(),接口返回:" + retObject.ToJson());
                    if (retObject["success"].ToString().ToLower() == "true")
                    {
                        //成功
                        _output.result = 1;
                        _output.code = retObject["code"].ToString();
                        _output.msg = "编辑人员成功";
                        _output.data = "";
                    }
                    else
                    {
                        //失败
                        _output.result = 0;
                        _output.code = retObject["code"].ToString();
                        _output.msg = retObject["msg"].ToString();
                        _output.data = "";
                    }
                }
            }
            catch (Exception ex)
            {
                _output.result = -1;
                _output.code = "err";
                _output.msg = "发生异常";
                _output.data = "";
                new LoggEx().errlog("调用宇泛接口->编辑人员,异常:" + ex);
            }
            new LoggEx().uniEvtlog(_output.ToJson());
            return _output;
        }
        /// <summary>
        /// 删除人员
        /// </summary>
        /// <param name="admitGuid">宇泛平台人员Id</param>
        /// <returns></returns>
        public dfpOutPut eyeDelete(string admitGuid, string CompanyId)
        {
            var _output = new dfpOutPut();
            try
            {
                var _appInfo = auth(CompanyId);
                if (_appInfo == null)
                {
                    _output.result = 0;
                    _output.code = "token_err";
                    _output.msg = "获取宇泛token失败";
                    _output.data = "";
                    return _output;
                }
                var _ubi_employee_delete = new employee_ubi_delete()
                {
                    admitGuids = admitGuid
                };
                new LoggEx().uniEvtlog("删除人员,->eyeDelete()->请求参数->" + _ubi_employee_delete.ToJson());
                var _CourseJson = JsonConvert.SerializeObject(_ubi_employee_delete);
                JObject retObject = WebApiHelper.Post(_ubi_apiurl.Replace("https", "http") + "/v2/admit/delete", _CourseJson, _appInfo.uni_token, _appInfo.uni_projectGuid);
                if (retObject != null)
                {
                    new LoggEx().uniEvtlog("调用宇泛接口->删除人员->eyeDelete(),接口返回:" + retObject.ToJson());
                    if (retObject["success"].ToString().ToLower() == "true")
                    {
                        //成功
                        _output.result = 1;
                        _output.code = retObject["code"].ToString();
                        _output.msg = "删除人员成功";
                        _output.data = "";
                    }
                    else
                    {
                        //失败
                        _output.result = 0;
                        _output.code = retObject["code"].ToString();
                        _output.msg = retObject["msg"].ToString();
                        _output.data = "";
                    }
                }
            }
            catch (Exception ex)
            {
                _output.result = -1;
                _output.code = "err";
                _output.msg = "发生异常";
                _output.data = "";
                new LoggEx().errlog("调用宇泛接口->删除人员,异常:" + ex);
            }
            new LoggEx().uniEvtlog(_output.ToJson());
            return _output;
        }
        #endregion

        #region 人像管理
        /// <summary>
        /// 注册人像
        /// </summary>
        /// <param name="admitGuid">宇泛平台人员Id</param>
        /// <param name="faceBase64">人像base64</param>
        /// <returns></returns>
        public dfpOutPut faceRegister(string admitGuid, string faceBase64, string CompanyId)
        {
            var _output = new dfpOutPut();
            try
            {
                var _appInfo = auth(CompanyId);
                if (_appInfo == null)
                {
                    _output.result = 0;
                    _output.code = "token_err";
                    _output.msg = "获取宇泛token失败";
                    _output.data = "";
                    return _output;
                }
                var _eyeFace_ubi = new eyeFace_ubi()
                {
                    admitGuid = admitGuid,
                    base64 = faceBase64,
                    faceTag = ""
                };
                new LoggEx().uniEvtlog("注册人像,->faceRegister()->请求参数->" + _eyeFace_ubi.ToJson());
                var _CourseJson = JsonConvert.SerializeObject(_eyeFace_ubi);
                JObject retObject = WebApiHelper.Post(_ubi_apiurl.Replace("https", "http") + "/v2/face/register", _CourseJson, _appInfo.uni_token, _appInfo.uni_projectGuid);
                if (retObject != null)
                {
                    new LoggEx().uniEvtlog("调用宇泛接口->注册人像->faceRegister(),接口返回:" + retObject.ToJson());
                    if (retObject["success"].ToString().ToLower() == "true")
                    {
                        //成功
                        _output.result = 1;
                        _output.code = retObject["code"].ToString();
                        _output.msg = "添加人像成功";
                        _output.data = retObject["data"].ToString();
                    }
                    else
                    {
                        //失败
                        _output.result = 0;
                        _output.code = retObject["code"].ToString();
                        _output.msg = retObject["msg"].ToString();
                        _output.data = "";
                    }
                }
            }
            catch (Exception ex)
            {
                _output.result = -1;
                _output.code = "err";
                _output.msg = "发生异常";
                _output.data = "";
                new LoggEx().errlog("调用宇泛接口->添加人像,异常:" + ex);
            }
            new LoggEx().uniEvtlog(_output.ToJson());
            return _output;
        }
        /// <summary>
        /// 注册人像
        /// </summary>
        /// <param name="admitGuid">宇泛平台人员Id</param>
        /// <param name="faceurl">人像图片地址</param>
        /// <param name="CompanyId">公司Id</param>
        /// <returns></returns>
        public dfpOutPut faceRegisterEx(string admitGuid, string faceurl, string CompanyId)
        {
            var _output = new dfpOutPut();
            try
            {
                var _appInfo = auth(CompanyId);
                if (_appInfo == null)
                {
                    _output.result = 0;
                    _output.code = "token_err";
                    _output.msg = "获取宇泛token失败";
                    _output.data = "";
                    return _output;
                }
                var _eyeFace_ubi = new eyeFace_ubi_url()
                {
                    admitGuid = admitGuid,
                    url = faceurl,
                    faceTag = ""
                };
                new LoggEx().uniEvtlog("注册人像->faceRegisterEx()->请求参数->uni_token:" + _appInfo.uni_token + ",uni_projectGuid:" + _appInfo.uni_projectGuid + "" + _eyeFace_ubi.ToJson());
                var _CourseJson = JsonConvert.SerializeObject(_eyeFace_ubi);
                JObject retObject = WebApiHelper.Post(_ubi_apiurl.Replace("https", "http") + "/v2/face/register", _CourseJson, _appInfo.uni_token, _appInfo.uni_projectGuid);
                if (retObject != null)
                {
                    new LoggEx().uniEvtlog("调用宇泛接口->添加人像->faceRegisterEx(),接口返回:" + retObject.ToJson());
                    if (retObject["success"].ToString().ToLower() == "true")
                    {
                        //成功
                        _output.result = 1;
                        _output.code = retObject["code"].ToString();
                        _output.msg = "添加人像成功";
                        _output.data = retObject["data"].ToString();
                    }
                    else
                    {
                        //失败
                        _output.result = 0;
                        _output.code = retObject["code"].ToString();
                        _output.msg = retObject["msg"].ToString();
                        _output.data = "";
                    }
                }
            }
            catch (Exception ex)
            {
                _output.result = -1;
                _output.code = "err";
                _output.msg = "发生异常";
                _output.data = "";
                new LoggEx().errlog("调用宇泛接口->添加人像->faceRegisterEx(),异常:" + ex);
            }
            new LoggEx().uniEvtlog(_output.ToJson());
            return _output;
        }
        /// <summary>
        /// 删除人像
        /// </summary>
        /// <param name="admitGuid">宇泛平台人员Id</param>
        /// <param name="faceGuid">人像Id</param>
        /// <returns></returns>
        public dfpOutPut faceDelete(string admitGuid, string faceGuid, string CompanyId)
        {
            var _output = new dfpOutPut();
            try
            {
                var _appInfo = auth(CompanyId);
                if (_appInfo == null)
                {
                    _output.result = 0;
                    _output.code = "token_err";
                    _output.msg = "获取宇泛token失败";
                    _output.data = "";
                    return _output;
                }
                var _eyeFace_ubi_delete = new eyeFace_ubi_delete()
                {
                    admitGuid = admitGuid,
                    faceGuid = faceGuid
                };
                new LoggEx().uniEvtlog("删除人像,请求参数->" + _eyeFace_ubi_delete.ToJson());
                var _CourseJson = JsonConvert.SerializeObject(_eyeFace_ubi_delete);
                JObject retObject = WebApiHelper.Post(_ubi_apiurl.Replace("https", "http") + "/v2/face/delete", _CourseJson, _appInfo.uni_token, _appInfo.uni_projectGuid);
                if (retObject != null)
                {
                    new LoggEx().uniEvtlog("调用宇泛接口->删除人像->faceDelete(),接口返回:" + retObject.ToJson());
                    if (retObject["success"].ToString().ToLower() == "true")
                    {
                        //成功
                        _output.result = 1;
                        _output.code = retObject["code"].ToString();
                        _output.msg = "删除人像成功";
                        _output.data = "";
                    }
                    else
                    {
                        //失败
                        _output.result = 0;
                        _output.code = retObject["code"].ToString();
                        _output.msg = retObject["msg"].ToString();
                        _output.data = "";
                    }
                }
            }
            catch (Exception ex)
            {
                _output.result = -1;
                _output.code = "err";
                _output.msg = "发生异常";
                _output.data = "";
                new LoggEx().errlog("调用宇泛接口->删除人像,异常:" + ex);
            }
            new LoggEx().uniEvtlog(_output.ToJson());
            return _output;
        }
        #endregion

        #region 设备权限
        /// <summary>
        /// 添加权限
        /// </summary>
        /// <param name="deviceNo">设备序列号</param>
        /// <param name="admitGuid">宇泛平台用户Id</param>
        /// <returns></returns>
        public dfpOutPut authdeviceAdd(string deviceNo, string admitGuid, string CompanyId)
        {
            var _output = new dfpOutPut();
            try
            {
                var _appInfo = auth(CompanyId);
                if (_appInfo == null)
                {
                    _output.result = 0;
                    _output.code = "token_err";
                    _output.msg = "获取宇泛token失败";
                    _output.data = "";
                    return _output;
                }
                var _passwordPermission = 1;
                if (admitGuid == "815F2BD7FC2C45808BED39338F44DF77")
                {
                    _passwordPermission = 2;
                }
                var _authdevice_ubi = new authdevice_ubi()
                {
                    deviceNo = deviceNo,
                    admitGuids = admitGuid,
                    permission = new permission
                    {
                        facePermission = 2,
                        idCardPermission = 2,
                        faceAndCardPermission = 1,
                        fingerPermission = 1,
                        idCardFacePermission = 1,
                        passwordPermission = _passwordPermission
                    }
                };
                var _CourseJson = JsonConvert.SerializeObject(_authdevice_ubi);
                JObject retObject = WebApiHelper.Post(_ubi_apiurl.Replace("https", "http") + "/v2/auth/device", _CourseJson, _appInfo.uni_token, _appInfo.uni_projectGuid);
                if (retObject != null)
                {
                    if (retObject["success"].ToString().ToLower() == "true")
                    {
                        //成功
                        _output.result = 1;
                        _output.code = retObject["code"].ToString();
                        _output.msg = "添加设备权限成功";
                        _output.data = "";
                    }
                    else
                    {
                        //失败
                        _output.result = 0;
                        _output.code = retObject["code"].ToString();
                        _output.msg = retObject["msg"].ToString();
                        _output.data = "";
                    }
                }
            }
            catch (Exception ex)
            {
                _output.result = -1;
                _output.code = "err";
                _output.msg = "发生异常";
                _output.data = "";
                new LoggEx().errlog("调用宇泛接口->添加设备权限,异常:" + ex);
            }
            new LoggEx().uniEvtlog(_output.ToJson());
            return _output;
        }
        /// <summary>
        /// 删除权限
        /// </summary>
        /// <param name="deviceNo">设备序列号</param>
        /// <param name="admitGuid">宇泛平台用户Id</param>
        public dfpOutPut authdeviceDelete(string deviceNo, string admitGuid, string CompanyId)
        {
            var _output = new dfpOutPut();
            try
            {
                var _appInfo = auth(CompanyId);
                if (_appInfo == null)
                {
                    _output.result = 0;
                    _output.code = "token_err";
                    _output.msg = "获取宇泛token失败";
                    _output.data = "";
                    return _output;
                }
                var _authdevice_ubi_delete = new authdevice_ubi_delete()
                {
                    deviceNo = deviceNo,
                    admitGuids = admitGuid
                };
                var _CourseJson = JsonConvert.SerializeObject(_authdevice_ubi_delete);
                JObject retObject = WebApiHelper.Post(_ubi_apiurl.Replace("https", "http") + "/v2/auth/device/revoke", _CourseJson, _appInfo.uni_token, _appInfo.uni_projectGuid);
                if (retObject != null)
                {
                    if (retObject["success"].ToString().ToLower() == "true")
                    {
                        //成功
                        _output.result = 1;
                        _output.code = retObject["code"].ToString();
                        _output.msg = "删除设备权限成功";
                        _output.data = "";
                    }
                    else
                    {
                        //失败
                        _output.result = 0;
                        _output.code = retObject["code"].ToString();
                        _output.msg = retObject["msg"].ToString();
                        _output.data = "";
                    }
                }
            }
            catch (Exception ex)
            {
                _output.result = -1;
                _output.code = "err";
                _output.msg = "发生异常";
                _output.data = "";
                new LoggEx().errlog("调用宇泛接口->删除设备权限,异常:" + ex);
            }
            new LoggEx().uniEvtlog(_output.ToJson());
            return _output;
        }
        #endregion
    }
}
